var SEARCH_HTML = '<style>#modal-serach_m{background: rgba(0,0,0,.5)} #modal-serach_m th{text-align:center !important;} #modal-serach_m td{text-align:center !important;} #modal-serach_view{display:none}</style>'+
	'<div id="modal-serach_m"  class="modal" tabindex="-1" aria-hidden="true">'+
    '<div class="modal-dialog" style="width: 520px;">'+
    '<form class="form form-horizontal">'+
      '  <div class="modal-content">'+
       '     <div class="modal-header"><h6 class="modal-title" id="search_mtitle"></h6><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" onclick="$(\'#modal-serach_m\').hide()"></button></div>'+
      '      <div class="modal-body">'+
     '          	<div class="row" id="modal-serach_view">'+
         '          <div class="input-group">'+
		'			<input class="form-control" type="text" id="SEARCH_MPLACE">'+
		'			<div class="input-group-btn"><a id="SEARCH_MFIND" class="btn btn-default">搜索</a></div>'+
      '         </div>'+
             '  	</div>'+
             '      <div style="max-height: 500px;overflow: auto;margin-top: 10px;">'+
            '      		<table id="search_mtable" class="table table-hover"></table>'+
            '  		</div>'+
           '       <div class="form-group" id="SEARCH_BTNS">'+
         '             <div style="text-align: center;">'+
        '                  <button type="button" class="btn btn-default" data-bs-dismiss="modal" onclick="$(\'#modal-serach_m\').hide()">取消</button>'+
       '                   <button type="button" id="SERACH_MSURE"  data-bs-dismiss="modal" class="btn btn-primary">确定</button>'+
     '             </div>'+
    '        </div>'+
   '     </div>'+
  '      </form>'+
 '   </div>'+
'</div>';

var SEARCH_CALLBACk;
var SEARCH_TYPE = 1;
var TABLE_WIDTH = 1;
/*
 * @param type 1:单选 2:多选
 * @param title 标题
 * @param placeholder 搜索框placeholder
 * @param headers 显示内容头部
 * @param data 显示数据 参数([{id:id,list:list,status:status}])
 * @param search 搜索函数 参数(关键字,接收数据函数)
 * @param callback 选中数据后回调
 * @param auto 打开自动搜索一次
 */
function SEARCH(type,title,placeholder,headers,data,search,callback,auto){
	$('#modal-serach_m').remove();
	$('body').append(SEARCH_HTML);
	SEARCH_TYPE = type;
	SEARCH_CALLBACk = callback;
	$('#modal-serach_m').show();
	$('#search_mtitle').text(title);
	$('#search_mtable').html('');
	$('#SEARCH_MFIND').parent().prev('input').attr('placeholder',placeholder);
	var ths = '<tr>';
	TABLE_WIDTH = 1;
	$.each(headers,function(k,v){
		ths += "<th>"+v+"</th>";
		TABLE_WIDTH++;
	});
	ths += "<th>选择</th></tr>";
	$('#search_mtable').append(ths);
	CREATE_TABLE_DATA(data);
	if(search){
		$("#modal-serach_view").show();
		$('#SEARCH_MFIND').click(function(){
			var kw = $(this).parent().prev('input').val();
			search(kw,CREATE_TABLE_DATA);
			$(this).parent().prev('input').val('');
		});
	}
	
	$('#SERACH_MSURE').click(function(){
		var arrs = new Array();
		$.each($('.search_mth td a'),function(k,v){
			if($(this).hasClass('btn-primary')){
				arrs.push(SEARCH_DATA_ARR[$(this).attr('data-id')]);
			}
		});
		if(type == 1){
			callback(arrs[0]);
		}else{
			callback(arrs);
		}
		$('#modal-serach_m').hide();
	});
	
	if(auto){
		$('#SEARCH_MFIND').trigger('click');
	}
	
}

var SEARCH_DATA_ARR;
function CREATE_TABLE_DATA(data){
	SEARCH_DATA_ARR = new Array();
	$('tr.search_mth').remove();
	var p = true;
	$.each(data,function(k,item){
		var t = "<tr class='search_mth'>";
		p = false;
		$.each(item['list'],function(i,v){
			t += "<td>"+v+"</td>";
		});
		var cs = "btn-default";
		if(item['status'] == 1) cs += " btn-primary";
		var dismiss = ' data-dismiss="modal" ';
		if(SEARCH_TYPE == 2) dismiss = '';
		t += "<td><a data-id="+item['id']+" "+dismiss+" class='btn "+cs+" btn-sm'>选取</a></td></tr>";
		$('#search_mtable').append(t);
		SEARCH_DATA_ARR[item['id']] = item;
	});
	
	if(p){
		var t = "<tr class='search_mth'>";
		t += "<td colspan='"+TABLE_WIDTH+"'>暂无搜索数据</td></tr>";
		$('#search_mtable').append(t);
	}
	
	$('.search_mth td a').click(function(){
		if(SEARCH_TYPE == 1){
			$('.search_mth td a').removeClass('btn-primary');
			$(this).addClass('btn-primary');
			$('#modal-serach_m').hide();
			$('.modal-backdrop').remove();
			SEARCH_CALLBACk(SEARCH_DATA_ARR[$(this).attr('data-id')]);
		}else{
			if($(this).hasClass('btn-primary')){
				$(this).removeClass('btn-primary').addClass('btn-default')
			}else{
				$(this).removeClass('btn-default').addClass('btn-primary')
			}			
		}
	});
}
